/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Util;

import java.util.List;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Nguyễn Thị Mỷ Diện (1142014)
 */
public class DataAccessUtil {
    public static List getList(String hql) throws Exception{
        // Mo session, ket noi
        Session sess = HibernateUtil.getSessionFactory().openSession();
        List list;
        list =  sess.createQuery(hql).list();
        sess.close();
        return list;
    }
    public static List getList(String hql, Map<String,Object> params) throws Exception{
        List list;
        Session sess = HibernateUtil.getSessionFactory().openSession();
        list =  sess.createQuery(hql)
                       .setProperties(params)
                       .list();
        sess.close();
        return list;
    }
    public static List getList(String hql, int limitStart, int limitRecords) throws Exception{
        // Mo session, ket noi
        Session sess = HibernateUtil.getSessionFactory().openSession();
        List list;
        // Tat ca cac giao tac thuc hien o day
        list =  sess.createQuery(hql).setMaxResults(limitRecords)
                                     .setFirstResult(limitStart).list();
        sess.close();
        return list;
    }
    public static List getList(String hql, Map<String,Object> params,
                    int limitStart, int limitRecords) throws Exception{
        List list;
        Session sess = HibernateUtil.getSessionFactory().openSession();
        list =  sess.createQuery(hql)
                            .setFirstResult(limitStart)
                            .setMaxResults(limitRecords)
                            .setProperties(params)
                            .list();
        sess.close();
        return list;
    }
    
     public static Object getFirstResult(String hql) throws Exception{
        // Mo session, ket noi
        Session sess = HibernateUtil.getSessionFactory().openSession();
        Object result;
        result =  sess.createQuery(hql).setFirstResult(0)
                                       .setMaxResults(1).list().get(0);
        sess.close();
        return result;
    }
     public static Object getFirstResult(String hql, Map<String,Object> params) throws Exception{
        // Mo session, ket noi
        Session sess = HibernateUtil.getSessionFactory().openSession();
        Object result;
        result =  sess.createQuery(hql).setProperties(params)
                                       .setFirstResult(0)
                                       .setMaxResults(1)
                                       .list().get(0);
        
        sess.close();
        return result;
    }
    
    
     public static Object getUniqueResult(String hql) throws Exception{
        // Mo session, ket noi
        Session sess = HibernateUtil.getSessionFactory().openSession();
        Object result;
        result =  sess.createQuery(hql).uniqueResult();
        sess.close();
        return result;
    }
     public static Object getUniqueResult(String hql, Map<String,Object> params) throws Exception{
        // Mo session, ket noi
        Session sess = HibernateUtil.getSessionFactory().openSession();
        Object result;
        result =  sess.createQuery(hql).setProperties(params)
                                       .uniqueResult();
        
        sess.close();
        return result;
    }
    
     public static int executeQuery(String hql) throws Exception{
        Session sess = HibernateUtil.getSessionFactory().openSession();
        int recordsEffected = 0;
        Transaction tx = null;
        try {
            tx = sess.beginTransaction();
            recordsEffected = sess.createQuery(hql)
                                   .executeUpdate();
            tx.commit();
        }
        catch (Exception e) {
            if (tx !=null) tx.rollback();
            throw e;
        }
        finally {
            sess.close();
        }
        return recordsEffected;
    }
      public static int executeQuery(String hql, Map<String,Object> params) throws Exception{
        Session sess = HibernateUtil.getSessionFactory().openSession();
        int recordsEffected = 0;
        Transaction tx = null;
        try {
            tx = sess.beginTransaction();
            recordsEffected = sess.createQuery(hql).setProperties(params)
                                                   .executeUpdate();
            tx.commit();
        }
        catch (Exception e) {
            if (tx !=null) tx.rollback();
            throw e;
        }
        finally {
            sess.close();
        }
        return recordsEffected;
    }
      
    public static boolean insert(Object o){
        Session sess = HibernateUtil.getSessionFactory().openSession();
        boolean success = false;
        Transaction tx = null;
        try {
            tx = sess.beginTransaction();
            sess.save(o);
            tx.commit();
            success = true;
        }
        catch (Exception e) {
            if (tx !=null) tx.rollback();
            throw e;
        }
        finally {
            sess.close();
        }
        return success;
    }
     public static boolean insert(List<Object> listObject){
        Session sess = HibernateUtil.getSessionFactory().openSession();
        boolean success = false;
        Transaction tx = null;
        try {
            tx = sess.beginTransaction();
            for(Object o : listObject){
                   sess.save(o);
            }
            tx.commit();
            success = true;
        }
        catch (Exception e) {
            if (tx !=null) tx.rollback();
            throw e;
        }
        finally {
            sess.close();
        }
        return success;
    }
     public static boolean update(Object o){
        Session sess = HibernateUtil.getSessionFactory().openSession();
        boolean success = false;
        Transaction tx = null;
        try {
            tx = sess.beginTransaction();
            sess.update(o);
            tx.commit();
            success = true;
        }
        catch (Exception e) {
            if (tx !=null) tx.rollback();
            throw e;
        }
        finally {
            sess.close();
        }
        return success;
    }
      public static boolean update(List listObject){
        Session sess = HibernateUtil.getSessionFactory().openSession();
        boolean success = false;
        Transaction tx = null;
        try {
            tx = sess.beginTransaction();
            for(Object o: listObject){
                 sess.update(o);
            }
            tx.commit();
            success = true;
        }
        catch (Exception e) {
            if (tx !=null) tx.rollback();
            throw e;
        }
        finally {
            sess.close();
        }
        return success;
    }
      public static boolean insertOrUpdate(Object o){
        Session sess = HibernateUtil.getSessionFactory().openSession();
        boolean success = false;
        Transaction tx = null;
        try {
            tx = sess.beginTransaction();
            sess.saveOrUpdate(o);
            tx.commit();
            success = true;
        }
        catch (Exception e) {
            if (tx !=null) tx.rollback();
            throw e;
        }
        finally {
            sess.close();
        }
        return success;
    }
      
       public static boolean delete(Object o){
        Session sess = HibernateUtil.getSessionFactory().openSession();
        boolean success = false;
        Transaction tx = null;
        try {
            tx = sess.beginTransaction();
            sess.delete(o);
            tx.commit();
            success = true;
        }
        catch (Exception e) {
            if (tx !=null) tx.rollback();
            throw e;
        }
        finally {
            sess.close();
        }
        return success;
    }
}
